package com.tjrac.generalpermissions.service.impl;

import com.tjrac.generalpermissions.dao.RolePermissionDao;
import com.tjrac.generalpermissions.domain.RolePermission;
import com.tjrac.generalpermissions.epception.MsgException;
import com.tjrac.generalpermissions.epception.statusCode.RolePermissionCode;
import com.tjrac.generalpermissions.service.RolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZeNing
 * @create 2020-12-01 14:56
 */
@Service
@Transactional
public class RolePermissionServiceImpl implements RolePermissionService {

    @Resource
    RolePermissionDao rolePermissionDao;

    @Override
    public List<RolePermission> queryRolePermissionByPid(int pid) throws MsgException {
        List<RolePermission> rolePermissions = rolePermissionDao.queryByRid(pid);
        return rolePermissions;
    }

    @Override
    public List<RolePermission> queryRolePermissionByRid(int rid) throws MsgException {
        List<RolePermission> rolePermissions = rolePermissionDao.queryByRid(rid);
        return rolePermissions;
    }

    @Override
    public String addRP(RolePermission rolePermission) throws MsgException {
        if (rolePermissionDao.addRolePermission(rolePermission) != 0) {
            return new MsgException(RolePermissionCode.SUCCESS_RolePermission, "添加权限角色映射表成功").toJson();
        } else {
            throw new MsgException(RolePermissionCode.FAILURE_ADD, "添加权限角色映射表失败");
        }
    }

    @Override
    public String delRP(int id) throws MsgException {
        if (rolePermissionDao.queryById(id) == null) {
            throw new MsgException(RolePermissionCode.NOT_EXIST_RolePermission, "该权限角色映射表不存在");
        } else {
            if (rolePermissionDao.delRolePermission(id) != 0) {
                return new MsgException(RolePermissionCode.SUCCESS_RolePermission, "删除权限角色映射表成功").toJson();
            } else {
                throw new MsgException(RolePermissionCode.FAILURE_DEL, "删除权限角色映射表失败");
            }
        }
    }

    @Override
    public String modifyRP(RolePermission rolePermission) throws MsgException {
        if (rolePermissionDao.queryById(rolePermission.getId()) == null) {
            throw new MsgException(RolePermissionCode.NOT_EXIST_RolePermission, "该权限角色映射表不存在,修改失败");
        } else {
            if (rolePermissionDao.modifyById(rolePermission) != 0) {
                return new MsgException(RolePermissionCode.SUCCESS_RolePermission, "修改权限角色映射表成功").toJson();
            } else {
                throw new MsgException(RolePermissionCode.FAILURE_UPD, "修改权限角色映射表失败");
            }
        }
    }
}
